package io.renren.common.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base32;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

public class GoogleAuthenticatorUtils {

	
	 /**

     * 时间前后偏移量

     * 用于防止客户端时间不精确导致生成的TOTP与服务器端的TOTP一直不一致

     * 如果为0,当前时间为 10:10:15

     * 则表明在 10:10:00-10:10:30 之间生成的TOTP 能校验通过

     * 如果为1,则表明在

     * 10:09:30-10:10:00

     * 10:10:00-10:10:30

     * 10:10:30-10:11:00 之间生成的TOTP 能校验通过

     * 以此类推

     */

    private static final int timeExcursion = 1;



    /**

     * 创建一个密钥

     */

    public static String createSecretKey() {

        SecureRandom random = new SecureRandom();

        byte[] bytes = new byte[20];

        random.nextBytes(bytes);

        Base32 base32 = new Base32();

        String secretKey = base32.encodeToString(bytes);

        return secretKey.toLowerCase();

    }

  


    /**

     * 校验方法

     *

     * @param secretKey 密钥

     * @param code      用户输入的TOTP

     */

    public static boolean verify(String secretKey, String code) {

        long time = System.currentTimeMillis() / 1000 / 30;

        for (int i = -timeExcursion; i <= timeExcursion; i++) {

            String totp = getTOTP(secretKey, time + i);

            if (code.equals(totp)) {

                return true;

            }

        }

        return false;

    }



    /**

	 * 根据密钥获取验证码

	 * 返回字符串是因为数值有可能以0开头

	 * @param secretKey 密钥

	 * @param time      第几个30秒 System.currentTimeMillis() / 1000 / 30

	 */

    public static String getTOTP(String secretKey, long time) {

        Base32 base32 = new Base32();

        byte[] bytes = base32.decode(secretKey.toUpperCase());

        String hexKey = Hex.encodeHexString(bytes);

        String hexTime = Long.toHexString(time);

        return TOTP.generateTOTP(hexKey, hexTime, "6");

    }



    /**

	 * 生成Google Authenticator二维码所需信息

	 * Google Authenticator 约定的二维码信息格式 : otpauth://totp/{issuer}:{account}?secret={secret}&issuer={issuer}

	 * 参数需要url编码 +号需要替换成%20

	 * @param secret  密钥 使用createSecretKey方法生成

	 * @param account 用户账户 如: example@domain.com 138XXXXXXXX

	 * @param issuer  服务名称 如: Google Github 印象笔记

	 */

	public static String createGoogleAuthQRCodeData(String secret, String account, String issuer) {

        String qrCodeData = "otpauth://totp/%s?secret=%s&issuer=%s";

		try {

			return String.format(qrCodeData, URLEncoder.encode(issuer + ":" + account, "UTF-8").replace("+", "%20"), URLEncoder.encode(secret, "UTF-8")

					.replace("+", "%20"), URLEncoder.encode(issuer, "UTF-8").replace("+", "%20"));

		} catch (UnsupportedEncodingException e) {

			e.printStackTrace();

		}

		return "";
    }

    public static String getQRBarcodeURL(String user, String host, String secret) {
        String format = "https://www.google.com/chart?chs=200x200&chld=M%%7C0&cht=qr&chl=otpauth://totp/%s@%s%%3Fsecret%%3D%s";
        return String.format(format, user, host, secret);
    }

    public boolean check_code(String secret, long code, long timeMsec) {
        Base32 codec = new Base32();
        byte[] decodedKey = codec.decode(secret);
        long t = (timeMsec / 1000L) / 30L;
        for (int i = -timeExcursion; i <= timeExcursion; ++i) {
            long hash;
            try {
                hash = verify_code(decodedKey, t + i);
            }catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
            if (hash == code) {
                return true;
            }
        }
        return false;
    }
    
    private static int verify_code(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException {
        byte[] data = new byte[8];
        long value = t;
        for (int i = 8; i-- > 0; value >>>= 8) {
            data[i] = (byte) value;
        }
        SecretKeySpec signKey = new SecretKeySpec(key, "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signKey);
        byte[] hash = mac.doFinal(data);
        int offset = hash[20 - 1] & 0xF;
        // We're using a long because Java hasn't got unsigned int.
        long truncatedHash = 0;
        for (int i = 0; i < 4; ++i) {
            truncatedHash <<= 8;
            // We are dealing with signed bytes:
            // we just keep the first byte.
            truncatedHash |= (hash[offset + i] & 0xFF);
        }
        truncatedHash &= 0x7FFFFFFF;
        truncatedHash %= 1000000;
        return (int) truncatedHash;
    }

}
